package org.deadline.model.baseapp;

import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.Iterator;
import java.util.List;
import java.util.Random;
import javax.validation.ConstraintViolation;
import javax.validation.ConstraintViolationException;
import org.deadline.model.config.EnumerationDataOnDemand;
import org.deadline.model.user.PersonDataOnDemand;
import org.deadline.repository.baseapp.UserPreferenceRepository;
import org.deadline.service.baseapp.UserPreferenceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Configurable;
import org.springframework.stereotype.Component;

@Configurable
@Component
public class UserPreferenceDataOnDemand {

	private Random rnd = new SecureRandom();

	private List<UserPreference> data;

	@Autowired
    private PersonDataOnDemand personDataOnDemand;

	@Autowired
    private EnumerationDataOnDemand enumerationDataOnDemand;

	@Autowired
    UserPreferenceService userPreferenceService;

	@Autowired
    UserPreferenceRepository userPreferenceRepository;

	public UserPreference getNewTransientUserPreference(int index) {
        UserPreference obj = new UserPreference();
        setConfigKey(obj, index);
        setConfigSection(obj, index);
        setConfigValues(obj, index);
        setCreationDate(obj, index);
        setDeleted(obj, index);
        setDescription(obj, index);
        setEnabled(obj, index);
        setModificationDate(obj, index);
        return obj;
    }

	public void setConfigKey(UserPreference obj, int index) {
        String configKey = "configKey_" + index;
        if (configKey.length() > 255) {
            configKey = configKey.substring(0, 255);
        }
        obj.setConfigKey(configKey);
    }

	public void setConfigSection(UserPreference obj, int index) {
        String configSection = "configSection_" + index;
        if (configSection.length() > 255) {
            configSection = configSection.substring(0, 255);
        }
        obj.setConfigSection(configSection);
    }

	public void setConfigValues(UserPreference obj, int index) {
        String configValues = "configValues_" + index;
        if (configValues.length() > 255) {
            configValues = configValues.substring(0, 255);
        }
        obj.setConfigValues(configValues);
    }

	public void setCreationDate(UserPreference obj, int index) {
        Date creationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setCreationDate(creationDate);
    }

	public void setDeleted(UserPreference obj, int index) {
        Boolean deleted = true;
        obj.setDeleted(deleted);
    }

	public void setDescription(UserPreference obj, int index) {
        String description = "description_" + index;
        if (description.length() > 255) {
            description = description.substring(0, 255);
        }
        obj.setDescription(description);
    }

	public void setEnabled(UserPreference obj, int index) {
        Boolean enabled = true;
        obj.setEnabled(enabled);
    }

	public void setModificationDate(UserPreference obj, int index) {
        Date modificationDate = new GregorianCalendar(Calendar.getInstance().get(Calendar.YEAR), Calendar.getInstance().get(Calendar.MONTH), Calendar.getInstance().get(Calendar.DAY_OF_MONTH), Calendar.getInstance().get(Calendar.HOUR_OF_DAY), Calendar.getInstance().get(Calendar.MINUTE), Calendar.getInstance().get(Calendar.SECOND) + new Double(Math.random() * 1000).intValue()).getTime();
        obj.setModificationDate(modificationDate);
    }

	public UserPreference getSpecificUserPreference(int index) {
        init();
        if (index < 0) {
            index = 0;
        }
        if (index > (data.size() - 1)) {
            index = data.size() - 1;
        }
        UserPreference obj = data.get(index);
        Long id = obj.getId();
        return userPreferenceService.findUserPreference(id);
    }

	public UserPreference getRandomUserPreference() {
        init();
        UserPreference obj = data.get(rnd.nextInt(data.size()));
        Long id = obj.getId();
        return userPreferenceService.findUserPreference(id);
    }

	public boolean modifyUserPreference(UserPreference obj) {
        return false;
    }

	public void init() {
        int from = 0;
        int to = 10;
        data = userPreferenceService.findUserPreferenceEntries(from, to);
        if (data == null) {
            throw new IllegalStateException("Find entries implementation for 'UserPreference' illegally returned null");
        }
        if (!data.isEmpty()) {
            return;
        }
        
        data = new ArrayList<UserPreference>();
        for (int i = 0; i < 10; i++) {
            UserPreference obj = getNewTransientUserPreference(i);
            try {
                userPreferenceService.saveUserPreference(obj);
            } catch (ConstraintViolationException e) {
                StringBuilder msg = new StringBuilder();
                for (Iterator<ConstraintViolation<?>> iter = e.getConstraintViolations().iterator(); iter.hasNext();) {
                    ConstraintViolation<?> cv = iter.next();
                    msg.append("[").append(cv.getConstraintDescriptor()).append(":").append(cv.getMessage()).append("=").append(cv.getInvalidValue()).append("]");
                }
                throw new RuntimeException(msg.toString(), e);
            }
            userPreferenceRepository.flush();
            data.add(obj);
        }
    }
}
